package com.qsl.ssyx.search.api;

import com.qsl.ssyx.common.result.Result;
import com.qsl.ssyx.model.search.SkuEs;
import com.qsl.ssyx.search.repository.SkuRepository;
import com.qsl.ssyx.search.service.SkuService;
import com.qsl.ssyx.vo.search.SkuEsQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@Api(tags = "ES接口")
@RestController
@RequestMapping("api/search/sku")
public class SkuApiController {

    @Autowired
    private SkuService skuService;


    @ApiOperation("上架商品")
    @GetMapping("inner/upperSku/{skuId}")
    public Result upperGoods(@ApiParam(value = "skuId", required = true)
                             @PathVariable Long skuId) {
        skuService.upperSku(skuId);
        return Result.ok(true);
    }

    @ApiOperation("下架商品")
    @GetMapping("inner/lowerSku/{skuId}")
    public Result lowerSku(@ApiParam(value = "skuId", required = true)
                           @PathVariable Long skuId) {
        skuService.lowerSku(skuId);
        return Result.ok(true);
    }

    @ApiOperation("获取10件爆款商品（热销好货）")
    @GetMapping("inner/findHotSkuList")
    public List<SkuEs> findHotSkuList() {
        List<SkuEs> skuEsList = skuService.findHotSkuList();
        return skuEsList;
    }

    @ApiOperation("获取分类商品-小程序分类信息")
    @GetMapping("{page}/{limit}")
    public Result getSkuList(@ApiParam(value = "当前页", required = true) @PathVariable Integer page,
                             @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
                             @ApiParam(value = "实体类", required = true) SkuEsQueryVo skuEsQueryVo) {
        // Spring Data分页， 0为第一页
        Pageable pageable = PageRequest.of(page - 1, limit);
        Page<SkuEs> pageModel = skuService.getSkuList(pageable, skuEsQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("根据skuId更新ES索引库的商品热度信息")
    @GetMapping("inerSkuHotScore/{skuId}")
    public void inerSkuHotScore(@ApiParam(value = "商品id", required = true) @PathVariable Long skuId) {
        skuService.incrSKuHotScore(skuId);
    }
}
